package com.pinyougou.sellergoods.server.implement;
import java.util.List;
import java.util.Map;

import com.pinyougou.group.Specification;
import com.pinyougou.mapper.TbSpecificationOptionMapper;
import com.pinyougou.pojo.TbSpecificationOption;
import com.pinyougou.pojo.TbSpecificationOptionExample;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pinyougou.mapper.TbSpecificationMapper;
import com.pinyougou.pojo.TbSpecification;
import com.pinyougou.pojo.TbSpecificationExample;
import com.pinyougou.pojo.TbSpecificationExample.Criteria;
import com.pinyougou.sellergoods.service.SpecificationService;

import com.pinyougou.utils.PageResult;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class SpecificationServiceImpl implements SpecificationService {

	@Autowired
	private TbSpecificationMapper specificationMapper;
	@Autowired
	private TbSpecificationOptionMapper optionMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<TbSpecification> findAll() {
		return specificationMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<TbSpecification> page=   (Page<TbSpecification>) specificationMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(Specification specification) {
		//1、添加规格
		specificationMapper.insert(specification.getSpec());
		//从前端页面传递过来的对象id属性为空，要修改mapper映射文件中的insert()方法，添加成功后为对应的实体类的id赋值
		//2、添加规格选项
		//2.1获取规格选项列表
		List<TbSpecificationOption> options = specification.getOptions();
		//2.2遍历规格选项列表，获取规格选项对象
		for (TbSpecificationOption option : options) {
			//2.3设置规格选项的specID的值
			option.setSpecId(specification.getSpec().getId());
			System.out.println("规格选项："+option);
			//2.4添加规格选项对象到数据库中
			optionMapper.insert(option);
		}
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(Specification specification){
		//1、修改规格
		specificationMapper.updateByPrimaryKey(specification.getSpec());
		//2、修改规格选项
		//第一步：删除该规格下对应的规格选项列表
		//2.1定义规格选项查询实例
		TbSpecificationOptionExample example = new TbSpecificationOptionExample();
		//2.2定义规格选项查询条件对象
		TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
		//2.3添加查询条件
		criteria.andSpecIdEqualTo(specification.getSpec().getId());
		//2.4根据specId删除该规格下对应的规格选项列表
		optionMapper.deleteByExample(example);
		//第二步：添加新的规格选项
		//2.5获取页面传递的规格选项列表
		List<TbSpecificationOption> options = specification.getOptions();
		//2.6遍历规格选项列表，获取规格选项对象
		for (TbSpecificationOption option : options) {
			//2.7设置规格选项的specId属性
			option.setSpecId(specification.getSpec().getId());
			//2.8添加规格选项
			optionMapper.insert(option);
		}
	}
	
	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public Specification findOne(Long id){
		//1、根据id查询规格对象
		TbSpecification tbSpecification = specificationMapper.selectByPrimaryKey(id);
		//2、根据specId查询规格选项列表
		//2.1定义查询实例
		TbSpecificationOptionExample exmple = new TbSpecificationOptionExample();
		//2.2定义查询条件对象
		TbSpecificationOptionExample.Criteria criteria = exmple.createCriteria();
		//2.3添加查询条件
		criteria.andSpecIdEqualTo(id);
		//2.4查询出规格选项列表
		List<TbSpecificationOption> options = optionMapper.selectByExample(exmple);
		//3、返回结果对象
		return new Specification(tbSpecification,options);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Long[] ids) {
		for(Long id:ids){
			specificationMapper.deleteByPrimaryKey(id);
		}		
	}

	/**
	 * 条件查询+分页
	 * @param specification
	 * @param pageNum 当前页 码
	 * @param pageSize 每页记录数
	 * @return
	 */
	@Override
	public PageResult findPage(TbSpecification specification, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		TbSpecificationExample example=new TbSpecificationExample();
		Criteria criteria = example.createCriteria();
		
		if(specification!=null){			
						if(specification.getSpecName()!=null && specification.getSpecName().length()>0){
				criteria.andSpecNameLike("%"+specification.getSpecName()+"%");
			}
		}
		Page<TbSpecification> page= (Page<TbSpecification>)specificationMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 查询所有规格列表
	 * @return
	 */
	@Override
	public List<Map> findAllSpecifications() {
		return specificationMapper.findAllSpecifications();
	}

}
